﻿package  {
	
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	public class XButtons extends Sprite {
		public static var instance:XButtons;
		
		public var buttons:Object;
		
		public function XButtons() {
			super();
			XButtons.instance = this;
			this.buttons = new Object();
			this.addEventListener(Event.ADDED_TO_STAGE, this.hOnStage);
		}
		private function hOnStage(ev:Event):void
		{
			trace("onstage");
			this.removeEventListener(Event.ADDED_TO_STAGE, this.hOnStage);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.hOnKeyDown);

		}
		private function hOnKeyDown(ev:KeyboardEvent):void
		{
			for( obj in this.buttons )
			{
				if(obj.key == ev.keyCode || obj.altKey == ev.keyCode)
				{
					obj.hRelease();
				}
			}
		}
		
		
		public static function linkButton( name:String, link:MovieClip, hRelease:Function = null, kKey:int = -1, kAltKey:int = -1 ):void
		{
			if(this.buttons[name])
				return;
			this.buttons[name] = new Object();
			this.buttons[name].link = link;
			this.buttons[name].hRelease = hRelease ? hRelease : function(){};
			this.buttons[name].key = kKey;
			this.buttons[name].altKey = kAltKey;
			link.actor.addEventListener(MouseEvent.CLICK, this.hOnMouseClick);
		}
		
		
		
		
		
		
	}
	
}
